import 'package:flutter/material.dart';
import 'package:hotfil_pub_web/pages/mine/widgets/publish_qr.dart';
import 'dart:typed_data';
import 'dart:io';
import 'dart:ui' as ui;
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:hotfil_pub_web/shared/loading.dart';
import 'package:hotfil_pub_web/shared/request_permission.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';

genPublishPost(context) async {
  var height = MediaQuery.of(context).size.height - 300;
  print(height);
  var width = 750 / 1624 * height;
  print(width);
  var repaintKey = GlobalKey(debugLabel: 'repaintKey');
  await showDialog(
      context: context,
      builder: (_) {
        return WillPopScope(
          onWillPop: () async => false,
          child: Dialog(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(10.0))),
            child: RepaintBoundary(
              key: repaintKey,
              child: Container(
                height: height,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(16),
                  color: Colors.white,
                ),
                child: Stack(
                  children: [
                    Image.asset('assets/images/publishQrBaseZh.png'),
                    Positioned(
                      bottom: 0,
                      child: ElevatedButton(
                        onPressed: () {
                          Navigator.pop(context);
                        },
                        child: Text('关闭'),
                      ),
                    ),
                    Positioned(
                        left: width / 2, top: height - 400, child: PublishQr()),
                    Positioned(
                        bottom: 0,
                        right: 0,
                        child: ElevatedButton(
                          onPressed: () async {
                            RenderRepaintBoundary? boundary =
                                repaintKey.currentContext?.findRenderObject()
                                    as RenderRepaintBoundary;
                            if (boundary == null) {
                              return;
                            }

                            double dpr = ui.window.devicePixelRatio;
                            ui.Image image =
                                await boundary.toImage(pixelRatio: dpr);

                            if (image == null) {
                              return;
                            }

                            ByteData? _byteData = await image.toByteData(
                                format: ui.ImageByteFormat.png);
                            if (_byteData == null) {
                              return;
                            }

                            await requestPhonePermission();

                            Uint8List sourceBytes =
                                _byteData.buffer.asUint8List();

                            final result =
                                await ImageGallerySaver.saveImage(sourceBytes);
                            if (result['isSuccess']) {
                              showSuccess(message: '保存成功');
                            } else {
                              showError(message: '保存失败');
                            }
                          },
                          child: Text('保存'),
                        ))
                  ],
                ),
              ),
            ),
          ),
        );
      });
}
